﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using ExamRepositoryWPF.Models;

namespace ExamRepositoryWPF
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private void ListView_Loaded(object sender, RoutedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv == null) return;
            MultiChoiceQuestion mq = (lv.Parent as FrameworkElement).DataContext as MultiChoiceQuestion;
            foreach(int ans in mq.Answer)
            {
                lv.SelectedItems.Add(mq.SectionsObject[ans]);
            }
        }

        private void ListView_Selected(object sender, RoutedEventArgs e)
        {
            ListBoxItem lbi = e.Source as ListBoxItem;
            if (lbi == null) return;
            MultiChoiceQuestion mq = (lbi.Parent as FrameworkElement).DataContext as MultiChoiceQuestion;
            mq.Answer.Add(mq.SectionsObject.IndexOf(lbi.DataContext.ToString()));
        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView lv = sender as ListView;
            if (lv == null) return;
            MultiChoiceQuestion mq = (lv.Parent as FrameworkElement).DataContext as MultiChoiceQuestion;
            mq.Answer.Clear();
            foreach(var si in lv.SelectedItems)
            {
                mq.Answer.Add(mq.SectionsObject.FindIndex((item) => item == si.ToString())); ;
            }
        }
    }
}
